package com.buaa.utility.servlet;

import java.io.IOException;
import javax.servlet.*;

public class CharacterEncodingSettingFilter implements Filter{
	public CharacterEncodingSettingFilter(){
        encoding = null;
        filterConfig = null;
	}
    public void destroy()
    {
        encoding = null;
        filterConfig = null;
    }

    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
        throws IOException, ServletException
    {
        String s = selectEncoding(servletrequest);
        if(s != null)
            servletrequest.setCharacterEncoding(s);
        filterchain.doFilter(servletrequest, servletresponse);
    }

    public void init(FilterConfig filterconfig)
        throws ServletException
    {
        filterConfig = filterconfig;
        encoding = filterconfig.getInitParameter("encoding");
    }

    protected String selectEncoding(ServletRequest servletrequest)
    {
        return encoding;
    }

    protected String encoding;
    protected FilterConfig filterConfig;
}
